package com.leejie.controller;

import com.leejie.bean.ExcelData;
import com.leejie.utils.ExcelUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class ExportController {

    public static final Logger log = LoggerFactory.getLogger(ExportController.class);

    @GetMapping("/expExcel")
    public void expExcel(HttpServletResponse response){
        ArrayList<String[]> strings = new ArrayList<>();
        strings.add(new String[]{"leejie","重庆","24","1898888888"});
        strings.add(new String[]{"zhangsan","北京","14","7878777777"});
        strings.add(new String[]{"zouhang","深圳","23","11111111"});
        ExcelData data = new ExcelData();
        data.setFileName("测试excel导出");
        data.setHead(new String[]{"name","addr","age","phone"});
        data.setData(strings);
        ExcelUtil.exportExcel(response,data);
    }

    @PostMapping("/impExcel")
    public List impExcel(MultipartFile file){
        List<Object[]> objects = ExcelUtil.importExcel(file);
        log.info("导出数据为：{}",objects.stream().collect(Collectors.toList()));
        return objects.stream().collect(Collectors.toList());
    }
}
